package com.example.dairenkun.neufoodshop.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.activity.EditUserActivity;

import com.example.dairenkun.neufoodshop.activity.MyCommentActivity;
import com.example.dairenkun.neufoodshop.activity.MyOrderActivity;
import com.example.dairenkun.neufoodshop.bean.User;
import com.example.dairenkun.neufoodshop.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;



public class MyselfFragment extends Fragment {
    private Button btn_edit;
    private Button btn_order;
    private Button btn_buy;
    private Button btn_comment;
    private TextView username;
    private TextView phone;

    private Context context;
    private  View view;
    String userid;
    public MyselfFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       view=inflater.inflate(R.layout.fragment_myself, container, false);
        btn_buy=(Button)view.findViewById(R.id.btn_buyrecord);
        btn_comment=(Button)view.findViewById(R.id.btn_comment);
        btn_order=(Button)view.findViewById(R.id.btn_myorder);
        btn_edit=(Button)view.findViewById(R.id.myself_edit);
        username=(TextView) view.findViewById(R.id.myself_name);
        phone=(TextView)view.findViewById(R.id.myself_phonenum);
        getUser();
        getData();
       return view;
    }
    public void getData(){

        //获取网上的数据/
        UserModel userModel=new UserModel();
        Call call=userModel.UserInfo(Integer.parseInt(userid));
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, retrofit2.Response<User > response)
            {
                final  User user=response.body();
               username.setText(user.getUsername());
                phone.setText(user.getMobilenum());
                //修改用户信息
                btn_edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent=new Intent(getActivity(), EditUserActivity.class);
                        intent.putExtra("user_id",user.getUser_id()+"");
                        intent.putExtra("username",user.getUsername());
                        intent.putExtra("userpass",user.getUserpass());
                        intent.putExtra("useraddress",user.getAddress());
                        intent.putExtra("userremake",user.getComment());
                        intent.putExtra("userphone",user.getMobilenum());
                        startActivity(intent);
                    }
                });
                //我的订单
                btn_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent=new Intent(getActivity(), MyOrderActivity.class);
                        intent.putExtra("user_id",user.getUser_id()+"");
                        startActivity(intent);
                    }
                });
                //我的评论
                btn_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(getActivity(), MyCommentActivity.class);
                        intent.putExtra("user_id",user.getUser_id()+"");
                        startActivity(intent);
                    }
                });
            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }

    private void getUser(){

        SharedPreferences sp = getActivity().getSharedPreferences("UserInfo", context.MODE_PRIVATE);
       userid = sp.getString("user_id", null);
    }

}

